using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class HinhThucThanhToan_DAL
    {
        private SqlConnection cn = null;
        public  HinhThucThanhToan_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(HinhThucThanhToan_DTO hinhThucThanhToan)
        {
            SqlCommand cm = new SqlCommand("tb_HINTHUCTHANHTOAN_INSERT", cn);
            cm.CommandType = CommandType.StoredProcedure;
            //cm.Parameters.AddWithValue("@MaHinhThucThanhToan", hinhThucThanhToan.MaHinhThucThanhToan);
            cm.Parameters.AddWithValue("@TENHINHTHUC_THANHTOAN", hinhThucThanhToan.TenHinhThucThanhToan);
            cm.Parameters.AddWithValue("@MoTa", hinhThucThanhToan.MoTa);
            cm.Parameters.AddWithValue("@HinhAnh", hinhThucThanhToan.HinhAnh);
            cm.ExecuteNonQuery();
        }
        public void Update(HinhThucThanhToan_DTO hinhThucThanhToan)
        {
            SqlCommand cm = new SqlCommand("tb_HinhThucThanhToan_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHinhThuc_ThanhToan", hinhThucThanhToan.MaHinhThucThanhToan);
            cm.Parameters.AddWithValue("@TenHinhThuc_ThanhToan", hinhThucThanhToan.TenHinhThucThanhToan);
            cm.Parameters.AddWithValue("@MoTa", hinhThucThanhToan.MoTa);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tb_HinhThucThanhToan_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHinhThuc_ThanhToan", iD);
            cm.ExecuteNonQuery();
        }
        public tbHinhThuc_ThanhToanColection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tb_HinhThucThanhToan_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbHinhThuc_ThanhToanColection hinhThucThanhToanCollection = new tbHinhThuc_ThanhToanColection();

            while (dr.Read())
            {
                HinhThucThanhToan_DTO hinhThucThanhToan = new HinhThucThanhToan_DTO();
                hinhThucThanhToan.MaHinhThucThanhToan = int.Parse(dr.GetValue(0).ToString());
                hinhThucThanhToan.TenHinhThucThanhToan = dr.GetValue(1).ToString();
                hinhThucThanhToan.MoTa = dr.GetValue(2).ToString();
                hinhThucThanhToan.HinhAnh = dr.GetValue(3).ToString();

                hinhThucThanhToanCollection.Add(hinhThucThanhToan);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hinhThucThanhToanCollection;
        }
    }
}
